package com.graduationproject.examonline.question.service;

import com.graduationproject.examonline.question.entity.Question;
import com.graduationproject.examonline.question.mapper.QuestionMapper;
import com.graduationproject.examonline.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    public Result getQuestionById(String id) {
        try {
            questionMapper.getQuestionById(id);
            return new Result(0, 200, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "查询失败");
        }
    }

    public Result insertQuestion(List<Map<String, Object>> questionList) {
        try {
            for (int index = 0; index < questionList.size(); index += 1) {
                Map<String, Object> question = questionList.get(index);
                List<Question> questionListOfFind = questionMapper.getQuestionById((String) question.get("question_id"));
                if (questionListOfFind.size() > 0) {
                    questionMapper.updateQuestionById(question);
                } else {
                    questionMapper.insertQuestion(question);
                }
            }
            return new Result(0, 200, "插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "插入失败");
        }
    }
}
